package com.raos.example.swagger.response;

import lombok.Data;

import java.io.Serializable;

/**
 * 统一消息响应体
 *
 * @author raos
 * @email 991207823@qq.com
 * @date 2022/6/21 20:24
 */
@Data
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /** 结果状态 */
    private int code;
    /** 响应消息 */
    private String msg;
    /** 响应数据 **/
    private T data;
    /** 接口请求时间 */
    private long timestamp;

    private R() {
        this.timestamp = System.currentTimeMillis();
    }

    public static <T> R<T> ok() {
        R<T> r = new R<>();
        r.setCode(ReturnCode.OK.getCode());
        r.setMsg(ReturnCode.OK.getMsg());
        return r;
    }

    public static <T> R<T> ok(String msg) {
        R<T> r = new R<>();
        r.setCode(ReturnCode.OK.getCode());
        r.setMsg(msg);
        return r;
    }

    public static <T> R<T> ok(T data) {
        R<T> r = new R<>();
        r.setCode(ReturnCode.OK.getCode());
        r.setMsg(ReturnCode.OK.getMsg());
        r.setData(data);
        return r;
    }

    public static <T> R<T> error() {
        R<T> r = new R<>();
        r.setCode(ReturnCode.ERR.getCode());
        r.setMsg(ReturnCode.ERR.getMsg());
        return r;
    }

    public static <T> R<T> error(String msg) {
        R<T> r = new R<>();
        r.setCode(ReturnCode.ERR.getCode());
        r.setMsg(msg);
        return r;
    }

    public static <T> R<T> error(int code, String msg) {
        R<T> r = new R<>();
        r.setCode(code);
        r.setMsg(msg);
        return r;
    }

}
